package jp.tuyano.sample.client.spreadsheet;

import jp.tuyano.sample.client.form.SSForm;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;

public class ConnectSpreadSheet implements EntryPoint {

	private final ConnectSpreadSheetServiceAsync csss =
		GWT.create(ConnectSpreadSheetService.class);
	
	private static final String SERVER_ERROR = "SERVER ERROR!!!!";
	
	private TextBox userName;
	private PasswordTextBox password;
	private Label label;
	private TextBox sheetName;
	private TextArea resultTextBox;
	
	public void onModuleLoad() {
		
		label = new Label(
				"Googleアカウントのメールアドレス、パスワードおよび\n" +
		"参照するスプレッドシート名を入力してください。");

		FlexTable table = new FlexTable();
		
		userName = new TextBox();
		password = new PasswordTextBox();
		sheetName = new TextBox();
		Button sendbutton = new Button("送信");
		resultTextBox = new TextArea();
		resultTextBox.setSize("768px", "300px");
		sendbutton.addClickHandler(new ClickHandler() {
			
			public void onClick(ClickEvent event) {
				SSForm form = new SSForm();
				form.setUserName(userName.getText());
				form.setPassword(password.getText());
				form.setSheetName(sheetName.getText());
				csss.connect(form, new AsyncCallback<String>() {
					
					public void onSuccess(String result) {
						// 戻された結果をresultに出力する
						resultTextBox.setText(result);
					}
					
					public void onFailure(Throwable caught) {
						
						resultTextBox.setText(SERVER_ERROR);
					}
				});
			}
		});

		table.setText(0, 0, "mail:");
		table.setText(1, 0, "password:");
		table.setText(2, 0, "spreadsheet name:");
		
		table.setWidget(0, 1, userName);
		table.setWidget(1, 1, password);
		table.setWidget(2, 1, sheetName);
		table.setWidget(3, 0, sendbutton);
		
		RootPanel.get("ss_sample_label").add(label);
		RootPanel.get("ss_sample_panel").add(table);
		RootPanel.get("ss_sample_result_panel").add(resultTextBox);
	}

}
